<?php
/* SVN FILE: $Id: lil_wysiwyg_plugin_controller.php 162 2009-10-14 19:48:36Z miha@nahtigal.com $ */
/**
 * Short description for lil_wysiwyg_plugin_controller.php
 *
 * Long description for lil_wysiwyg_plugin_controller.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil_wysiwyg_plugin
 * @subpackage    lil_wysiwyg_plugin
 * @since         v 1.0
 * @version       $Revision: 162 $
 * @modifiedby    $LastChangedBy: miha@nahtigal.com $
 * @lastmodified  $Date: 2009-10-14 21:48:36 +0200 (sre, 14 okt 2009) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
App::import('Controller', 'Lil.LilPlugin');
/**
 * LilWysiwygPlugin class
 *
 * @uses          LilWysiwygPluginController
 * @package       lil_wysiwyg_plugin
 * @subpackage    lil_wysiwyg_plugin
 */
class LilWysiwygPluginController extends LilPluginController {
	public $name = 'LilWysiwygPlugin';
/**
 * handlers property
 *
 * @var array
 * @access public
 */
	public $handlers = array(
		'form_edit_post' => array('function' => '_initWysiwyg', 'params' => array()),
		'form_add_post' => array('function' => '_initWysiwyg', 'params' => array())
	);
/**
 * _initWysiwyg method
 *
 * This replaces textarea with CKEditor and sets properties
 *
 * @param mixed $model
 * @access public
 * @return void
 */
	function _initWysiwyg($view, $form_data) {
		App::uses('HtmlHelper', 'View/Helper');
		$Html = new HtmlHelper($view);
		App::uses('LilHelper', 'Lil.View/Helper');
		$Lil = new LilHelper($view);
		
		$definition = array(
			$Html->script('/lil_wysiwyg/js/ckeditor/ckeditor.js') . PHP_EOL, 
			'<script type="text/javascript">' . PHP_EOL,
			'ckeditor_styles_start' => '	CKEDITOR.addStylesSet(\'lilblogs_styles\', [' . PHP_EOL,
			'		{ name : \'' . __d('lil_wysiwyg', 'Normal', true) . '\', element : \'div\' },' . PHP_EOL,
			'		{ name : \'' . __d('lil_wysiwyg', 'Paragraph 1', true) . '\', element : \'h1\' },' . PHP_EOL,
			'		{ name : \'' . __d('lil_wysiwyg', 'Paragraph 2', true) . '\', element : \'h2\' },' . PHP_EOL,
			'		{ name : \'' . __d('lil_wysiwyg', 'Paragraph 3', true) . '\', element : \'h3\' },' . PHP_EOL,
			'		{ name : \'' . __d('lil_wysiwyg', 'Caption', true) . '\', element : \'div\', attributes: {\'class\': \'caption\'} },' . PHP_EOL,
			'		{ name : \'' . __d('lil_wysiwyg', 'Preformatted', true) . '\', element : \'pre\' }' . PHP_EOL,
			'ckeditor_styles_end' => '	]);' . PHP_EOL,
			'ckeditor_settings_start' => '	CKEDITOR.replace(\'data[Post][body]\', {' . PHP_EOL,
			'		toolbar: [[\'Styles\',\'-\',\'Bold\',\'Italic\',\'Underline\',\'Strike\',\'-\',\'NumberedList\',\'BulletedList\',\'-\',\'Link\',\'Unlink\',\'Image\',\'Table\',\'-\',\'Undo\',\'Redo\',\'RemoveFormat\',\'-\',\'Source\']], ' . PHP_EOL,
			'		extraPlugins: \'autogrow\',' . PHP_EOL,
			'		removePlugins: \'resize\',' . PHP_EOL,
			'		entities: false,' . PHP_EOL,
			'		width: 600,' . PHP_EOL,
			'		resize_maxWidth: 600,' . PHP_EOL,
			'		resize_minWidth: 600,' . PHP_EOL,
			'		autoGrow_onStartup  : true,' . PHP_EOL,
			'		autoGrow_minHeight : 200,' . PHP_EOL,
			'		customConfig : \'\',' . PHP_EOL,
			'		scayt_autoStartup : false,' . PHP_EOL,
			'		stylesCombo_stylesSet : \'lilblogs_styles\',' . PHP_EOL,
			'ckeditor_settings_end' => '		dummyElement : false' . PHP_EOL,
			'	});' . PHP_EOL,
			'	CKEDITOR.on(\'dialogDefinition\', function( ev ) {' . PHP_EOL,
			'		var dialogName = ev.data.name;' . PHP_EOL,
			'		var dialogDefinition = ev.data.definition;' . PHP_EOL,
			'		if ( dialogName == \'link\' ) {' . PHP_EOL,
			'			dialogDefinition.removeContents( \'advanced\' );' . PHP_EOL,
			'			dialogDefinition.removeContents( \'target\' );' . PHP_EOL,
			/*'			var infoTab = dialogDefinition.getContents( \'info\' );' . PHP_EOL,
			'			var linkTypeField = infoTab.get( \'linkType\' );' . PHP_EOL,
			'			linkTypeField.items.push([ \'Posts\', \'posts\' ]);' . PHP_EOL,*/
			'		};' . PHP_EOL,
			'		if ( dialogName == \'image\' ) {' . PHP_EOL,
			'			dialogDefinition.removeContents( \'advanced\' );' . PHP_EOL,
			'		};' . PHP_EOL,
			'	});' . PHP_EOL,
			'</script>'
		);
		
		if (in_array('Media', (array)Configure::read('Lil.plugins'))) {
			$media_settings = array(
				'		filebrowserBrowseUrl : \'' . Router::url(array('plugin' => 'lil_media', 'admin' => true, 'controller' => 'media', 'action' => 'browse')) . '\',' . PHP_EOL,
				'		filebrowserWindowWidth : 640,' . PHP_EOL,
				'		filebrowserWindowHeight : 480,' . PHP_EOL,
				'		filebrowserImageBrowseUrl : \'' . Router::url(array('plugin' => 'lil_media', 'admin' => true, 'controller' => 'media', 'action' => 'browse')) . '\',' . PHP_EOL,
				'		filebrowserImageWindowWidth : 640,' . PHP_EOL,
				'		filebrowserImageWindowHeight : 480,' . PHP_EOL
			);
			
			$Lil->insertIntoArray($definition, $media_settings, array('before' => 'ckeditor_settings_end', 'preserve' => false));
		}
		
		$form_data['form']['post'] = array_merge((array)$form_data['form']['post'], $definition);
		return $form_data;
	}

}